using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Text;

namespace AresControls
{
	public class HSelect : HControl
	{
		private string m_DefaultStyle = "", m_SessionKey = "";
		private ArrayList m_Code = null, m_Text = null;
		private int m_CellIndex;

		public HSelect()
		{
			m_DefaultStyle = ";font-size:8pt;font-family:Arial;margin:0;border:0;height:18px;color:#442200;background-color:#FFFFFF;text-align:left;";
		}

		public int CellIndex
		{
			get { return m_CellIndex; }
			set { m_CellIndex = value; }
		}

		public ArrayList Code
		{
			get{ return (SessionKey == "")?null:(ArrayList)(((object[])Context.Session[SessionKey])[0]); }
			set { m_Code = value; }
		}

		public ArrayList Text
		{
			get{ return (SessionKey == "")?null:(ArrayList)(((object[])Context.Session[SessionKey])[1]); }
			set { m_Text = value; }
		}

		private string SessionKey
		{
			get{ return m_SessionKey; }
			set { m_SessionKey = value; }
		}

		protected override void Render(HtmlTextWriter output)
		{
			StringBuilder sb = new StringBuilder();
			SessionKey = GridId + "Column"+ CellIndex.ToString();

			if (Page.IsPostBack)
			{
				Code = (ArrayList)((object[])Context.Session[SessionKey])[0];
				Text = (ArrayList)((object[])Context.Session[SessionKey])[1];
			}
			else
				WriteGridVariableOnSession(SessionKey, new object[]{m_Code, m_Text});

			if (!Required)
			{
				m_Text = FillArray(m_Text);
				m_Code = FillArray(m_Code);
			}

			sb.Append("<input ControlType=\"HSelect\" ");
			sb.Append((ToolTip)?renderToolTip():"");
			sb.Append((ReadOnly)?renderControlReadOnly():"");
			Style += (ReadOnly)?";background-color:"+ReadOnlyColor:"";
			sb.Append((DefaultValue != null)?renderDefaultValue():"");
			sb.Append((Required)?renderRequired():"");
			sb.Append((GridControl)?renderGridControl():"");
			sb.Append((InvokeOnChange != "")?renderInvokeOnChange():"");
			sb.Append((InvokeOnBeforeDeactivate != "")?renderInvokeOnBeforeDeactivate():"");
			sb.Append((InvokeOnActivate != "")?renderInvokeOnActivate():"");
			sb.Append((InvokeOnChangeLine != "")?renderInvokeOnChangeLine():"");
			sb.Append((AlertRequiredString != "")?" AlertRequiredString=\"" + AlertRequiredString + "\" ":"");
			sb.Append(renderGridID());
			sb.Append(" style=\"width:" + Width + m_DefaultStyle + Style + "\" ");
			sb.Append(" ondragenter=\"event.returnValue=false;\" onactivate=\"OnHSelectFocus(this);\" />");

			sb.Append("<select onkeydown=\"OnHControlKeyDown(this.parentElement.childNodes(0));\" onselectstart=\"event.returnValue=false\" ");
			sb.Append(" onchange=\"OnHSelectChange(this);\" tabindex=-1 ondeactivate=\"OnHSelectBlur(this);\" FirstTime=\"true\" CodeValue=\"\" ");
			sb.Append(renderGridID());
			sb.Append(" style=\"display:none;width:" + int.Parse(Width) + m_DefaultStyle + Style + "\" >");

			int i = 0;
			while (i<m_Code.Count)
				sb.Append("<option value=\'" + m_Code[i] + "\'>" + ConvertBlank(m_Text[i++].ToString()));
			sb.Append("</select>");
			output.Write(sb.ToString());
		}

		private ArrayList FillArray(ArrayList arr)
		{
			ArrayList arrValues = arr;
			arr = new ArrayList();
			arr.Add("");
			int i = 0;
			while(i<arrValues.Count)
				arr.Add(arrValues[i++]);
			return arr;
		}
		private string ConvertBlank(string strSource)
		{
			return strSource.Replace((char)32,(char)160);
		}

		private ArrayList Session_TempGridVars
		{
			get
			{
				try
				{
					ArrayList retVal = (ArrayList)(Context.Session[HGrid.KEY_TEMP_GRID_VARS]);
					if (retVal == null)
					{
						retVal = new System.Collections.ArrayList();
						Context.Session.Add(HGrid.KEY_TEMP_GRID_VARS, retVal);
					}
					return retVal;
				}
				catch
				{
					return null;
				}
			}
			set
			{
				if (null == value)
				{
					Context.Session[HGrid.KEY_TEMP_GRID_VARS] = null;
					Context.Session.Remove(HGrid.KEY_TEMP_GRID_VARS);
				}
				else
				{
					ArrayList retVal = (ArrayList)(Context.Session[HGrid.KEY_TEMP_GRID_VARS]);
					if (null == retVal)
						Context.Session.Add(HGrid.KEY_TEMP_GRID_VARS, value);
					else
						Context.Session[HGrid.KEY_TEMP_GRID_VARS] = value;
				}
			}
		}
		private void WriteGridVariableOnSession(string strTagName, object objSes)
		{
			Context.Session[strTagName] = objSes;
			for (int i=0; i<Session_TempGridVars.Count; ++i)
				if (Session_TempGridVars[i].ToString() == strTagName)
					return;

			Session_TempGridVars.Add(strTagName);
		}
	}
}
